package com.chance.cc.crawler.prod.command.job.domain.vm.xcar.realtime;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.CrawlerJob;
import com.chance.cc.crawler.core.downloader.HttpConfig;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.filter.FilterUtils;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.meta.core.bean.CrawlerMetaConstant;
import com.chance.cc.crawler.meta.core.bean.job.CrawlerScheduleJob;
import com.chance.cc.crawler.prod.command.job.domain.vm.xcar.XCarAutoCommonCrawlerSchedulerJob;
import org.apache.commons.lang3.StringUtils;

import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRequestType.*;

/**
 * @ClassName song
 * @Description TODO
 * @Author ding
 * @Date 2021/8/29 10:24
 * 爱卡车系 车型采集
 * @Version 1.0
 **/
public class XcarSeriesGatherRealTimeCrawlerScheduleJob  extends XCarAutoCommonCrawlerSchedulerJob {
    public static String site = "gather";
    public static void main(String[] args) {
        publishXcarNewsSchedulerJob();
    }

    public static CrawlerJob publishXcarNewsSchedulerJob(){
        CrawlerJob crawlerJob = xcarCwalerJob();


        //发布定时采集作业
        CrawlerScheduleJob crawlerScheduleJob = new CrawlerScheduleJob();
        crawlerScheduleJob.setDomain(domain);
        crawlerScheduleJob.setCrawlerJob(JSON.toJSONString(crawlerJob));
        crawlerScheduleJob.setJobType(CrawlerMetaConstant.ScheduleCrawlerJobType.crawler.enumVal());
        crawlerScheduleJob.setNote("爱卡汽车车系配置信息每月1号5点 定时采集");//TODO 定时采集时间未定
        crawlerScheduleJob.setCrawlerKey(crawlerJob.generateCrawlerKey());
        HttpPage page = metaServiceCommand.addOrUpdateCrawlerScheduleJob(crawlerScheduleJob);
        System.out.println("发布作业：" + page.getRawText());

        return crawlerJob;
    }

    public static CrawlerJob xcarCwalerJob() {
        String url = "https://newcar.xcar.com.cn/";
        String site_biz = site + "-realtime";//实时
        HttpConfig httpConfig = HttpConfig.me(domain);
        httpConfig.setHttpSite(site);

        CrawlerRequestRecord record = CrawlerRequestRecord.builder()
                .startPageRequest(domain, CrawlerEnum.CrawlerRequestType.turnPage)
                .httpConfig(httpConfig)
                .httpUrl(url)
                .proxy(proxy)
                .httpHead("user-agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36")
                .domain(domain)
                .releaseTime(System.currentTimeMillis())
                .filter(CrawlerEnum.CrawlerRecordFilter.dateRange)
                .addFilterInfo(FilterUtils.dateRangeFilterInfo(24,null))
                .resultLabelTag(CrawlerEnum.CrawlerDataType.article)
                .build();
        record.tagsCreator().bizTags().addDomain(domain);
        record.tagsCreator().bizTags().addSite(site);
        record.tagsCreator().bizTags().addSiteBiz(site_biz);
        record.getHttpRequest().setResponseCharset("UTF-8");

        //车系关键次
        CrawlerRequestRecord initNewsCrawlerRecord = CrawlerRequestRecord.builder()
                .startPageRequest("xcar_series",turnPageItem)
                .httpUrl("http://192.168.1.217:9599/v1/meta/xcar/keys?site=series_name")//http://192.168.1.217:9959/v1/meta/xcar/keys?site=carSeries
                .requestLabelTag(supportSource)
                .requestLabelTag(internalDownload)
                .build();

        CrawlerJob crawlerJob = CrawlerJob.builder()
                .crawlerJobThreadNumber(10)
                .triggerInfo(
                        domain,
                        CrawlerMetaConstant.ScheduleJobTrigger_Cron,
                        System.currentTimeMillis(),//xcar-gather-realtime-realtime
                        StringUtils.joinWith("-", domain, site,"realtime", CrawlerMetaConstant.ScheduleJobTriggerJob_Realtime))
                .crawlerRequestQueue(CrawlerMetaConstant.redisRequestQueue(StringUtils.joinWith("-",domain,site,site_biz,"queue")))
                .kafkaResultPipeline("kafka", kafkaTopciForTraceJob, null)
                .requestRecord(record)
                .supportRecord(initNewsCrawlerRecord)
                .build();
        return crawlerJob;
    }
}
